﻿#include <iostream>
#include <thread>
#include <functional>
#include "think.h"
#include <conio.h>

#pragma comment( lib, "think.lib" )
#pragma comment( lib, "Ws2_32.lib" )

void recv_thread(THINK_NET* net)
{
    char buf[1024];
    int len;

    while ((len = (think_netrecv(net, buf, sizeof(buf), 0))) > 0) {
        std::cout << std::string(buf,len);
    }
    exit(0);
}

int main(int argc, char* argv[])
{
    THINK_NET* net;

    if (argc != 3) {
        std::cout << "usage: mytelnet ip port" << std::endl;
        std::cout << "example:mytelnet 127.0.0.1 10000" << std::endl;
        return 0;
    }
    think_netstart();
    if (!(net = think_netconnect(argv[1], atol(argv[2])))) {
        think_showerror();
        return 0;
    }

    std::thread t(std::bind(recv_thread,net));

    char ch;
    while (true) {
        ch = _getch();
        if (ch == 0x04) // ^D
            break;
        think_netsend(net, &ch, 1, 0);
    }
    think_netclose(net);
    t.join();

    return 0;
}
